// Log a click
var clk = function(url,query,ctype,cdata,rank,start,frontend){
    if(!document.images) {
        return true;
    }
    var esc = encodeURIComponent || escape;
    var img = document.createElement('img');

    var src = "http://lpcsandbox.appspot.com/click?" + 
        (query 		? "&q=" + esc(query) : "") +
        (ctype 		? "&ct=" + esc(ctype) : "") + 
	(cdata 		? "&cd=" + esc(cdata) : "") + 
	(url   		? "&url=" + esc(url.replace(/#.*/,"")).replace(/\+/g,"%2B") : "") + 
	(frontend	? "&fend=" + esc(frontend) : "");

    if (typeof rank != 'undefined') {
        src += "&r=" + esc(rank);
    }
    if (typeof start != 'undefined') {
        src += "&start=" + esc(start);
    }


    img.src=src;
    return true
}


// Log click on a link by picking out appropriate
// attributes in the element that describe the link.
var link_clicked = function() {

  var cdata = this.getAttribute("cdata");
  var ctype = this.getAttribute("ctype");
  var rank = this.getAttribute("rank");  
  var frontend = this.getAttribute("frontend");
  
  if (!ctype) {
    ctype = "OTHER"; // unknown link type
  }

  var url;
  if (this.href) {
    url = this.href;
  } else {
    url = "#"; // link w/o href
  }

  clk(url, page_query, ctype, cdata, rank, page_start, frontend);
}

// Attach onmousedown handlers to all links
var ar = document.getElementsByTagName("a");
var arlen = ar.length;
for (var i = 0; i < arlen; i++) {
  var el = ar[i];
  if (! el.onmousedown) {
    el.onmousedown = link_clicked;
  }
}